package ru.ipccenter.jshell.frontend.shell;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Collection;

import ru.ipccenter.jshell.InterprocessStream;

public class ShellInternalImpl implements ShellInternal {

    InputStream  input;
    OutputStream output;
    OutputStream error;
    private File workingdirectory;
    private HashMap<String,Object> envv;
    public ShellInternalImpl(InputStream input, OutputStream output, OutputStream error) {
        
        this.input  = input;
        this.output = output;
        this.error  = error;
        workingdirectory=new File(System.getProperty("user.dir"));
        envv=new HashMap<String,Object>();
    }
    
    @Override
    public InterprocessStream read() {
        
        return new InterprocessStreamInternalImpl(getInputSocketStream(), this); 
    }

    @Override
    public InterprocessStream read(String file) throws IOException {
        if (isAbsolutePath(file)) {
            return new InterprocessStreamInternalImpl(new FileInputStream(file), this);
        } else {
            return new InterprocessStreamInternalImpl(new FileInputStream(new File(workingdirectory, file)), this);
        }
    }

    @Override
    public InterprocessStream exec(String... args) {
        
        InterprocessStreamInternal ips = 
            new InterprocessStreamInternalImpl(new ByteArrayInputStream(new byte[0]), this);
        return Executor.exec(ips, this, args, null, null);
    }
    public InterprocessStream exec(String[] args,File directory) {
        
        InterprocessStreamInternal ips = 
            new InterprocessStreamInternalImpl(new ByteArrayInputStream(new byte[0]), this);
        return Executor.exec(ips, this, args, null, directory);
    }

    @Override
    public void cd(String path)
    {
        if(isAbsolutePath(path)){
        workingdirectory=new File(path);
        } else{
            workingdirectory=new File(workingdirectory,path);
        }
        if(!workingdirectory.isDirectory()){
            //TODO error handling
        }
        if(isWindows()){
            
            String[] args={"cmd","/c","cd", path};
            exec(args,workingdirectory);
        }
        if(isUnix()){
            String[] args={"/bin/bash", "-c", "cd", path};
            exec(args,workingdirectory);
        }
    }

    @Override
    public void pwd()
    {
        if(isWindows()){
            String[] args={"cmd","/c","echo","%cd%"};
            exec(args,workingdirectory).write();//????
            
        }
        
        if(isUnix()){
           String[] args={"/bin/bash", "-c","pwd"};
           exec(args,workingdirectory).write();
           
        }
    }
     
    @Override 
    public Object getEnv(String key){
        return envv.get(key);
    }
    
    @Override
    public void setEnv(String key,Object o){
        envv.put(key,o);
    }
    
    @Override
    public Collection<Object> Env(){
        return envv.values();
    }
    
    @Override 
    public InputStream getInputSocketStream() {
        
        return input;
    }

    @Override
    public OutputStream getOutputSocketStream() {

        return output;
    }

    @Override
    public OutputStream getErrorSocketStream() {

        return error;
    }
    
    public static boolean isWindows(){

        String os = System.getProperty("os.name").toLowerCase();
        //windows
        return (os.indexOf( "win" ) >= 0); 
    }
    
    public static boolean isUnix (){

        String os = System.getProperty("os.name").toLowerCase();
        //linux or unix
        return (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0);

    }
    
    public static boolean isAbsolutePath(String Path){
    if(isWindows()){
        if(Path.contains(":")){
            return true;
            }
        return false;
    }
    if(isUnix()){
        if(Path.startsWith("/")){
            return true;
        }
        return false;
    }
    return false;
    }

}
